/*-*
 * FILENAME  :
 *    $HeadURL$
 *
 * STATUS  :
 *    $Revision$
 *
 *    $Author$
 *    $Date$
 *  
 *    
 * Copyright (c) 2011 Gonalge Ltda. All rights reserved.
 *
 ****************************************************************/

package org.gonalge.arbolrojonegro.presentacion;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

/**
 * 
 * @author Geremias Gonzalez
 */
public class PintarFractal extends Canvas {

    /**
     * 
     */
    private static final long serialVersionUID = -4559588306563051524L;

    private int cont = 5;

    private int alfa = 90;

    public PintarFractal(int alfa) {
        this.alfa = alfa;
    }

    public void pintar() {
        repaint();
        cont = (cont < 6) ? cont + 1 : 5;
    }

    private double seno(int alfa) {
        return Math.sin(Math.PI * alfa / 180);
    }

    private double coseno(int alfa) {
        return Math.cos(Math.PI * alfa / 180);
    }

    @Override
    public void paint(Graphics g) {
        arbolNavidad(g);

    }

    private double distancia(Point p1, Point p2) {
        return (Math.sqrt(((p2.x - p1.x) * (p2.x - p1.x)) + ((p2.y - p1.y) * (p2.y - p1.y))));
    }

    void arbolNavidad(Graphics g) {
        g.setColor(Color.black);
        arbolitoDeNavidad(g, new Point(110, 20), new Point(110, 350), alfa, cont);

    }

    void arbolitoDeNavidad(Graphics g, Point p1, Point p2, int alfa, int n) {
        Point p3 = new Point();
        Point p4 = new Point();
        Point p5 = new Point();
        Point p6 = new Point();
        Point p7 = new Point();
        Point p8 = new Point();
        Point p9 = new Point();
        Point p10 = new Point();
        Point p11 = new Point();
        double d;
        g.drawLine(p1.x, p1.y, p2.x, p2.y);
        if (n > 1) {
            d = distancia(p1, p2);

            p4.x = (p1.x + p2.x) / 2;
            p4.y = (p1.y + p2.y) / 2;
            p3.x = (p2.x + p4.x) / 2;
            p3.y = (p2.y + p4.y) / 2;
            p5.x = (p1.x + p4.x) / 2;
            p5.y = (p1.y + p4.y) / 2;

            p6.x = p3.x + (int) ((d / 2) * coseno((alfa - 30)));
            p6.y = p3.y - (int) ((d / 2) * seno((alfa - 30)));
            p7.x = p4.x + (int) ((d / 3) * coseno((alfa - 30)));
            p7.y = p4.y - (int) ((d / 3) * seno((alfa - 30)));
            p8.x = p5.x + (int) ((d / 4) * coseno((alfa - 30)));
            p8.y = p5.y - (int) ((d / 4) * seno((alfa - 30)));
            p9.x = p3.x + (int) ((d / 2) * coseno((alfa + 30)));
            p9.y = p3.y - (int) ((d / 2) * seno((alfa + 30)));
            p10.x = p4.x + (int) ((d / 3) * coseno((alfa + 30)));
            p10.y = p4.y - (int) ((d / 3) * seno((alfa + 30)));
            p11.x = p5.x + (int) ((d / 4) * coseno((alfa + 30)));
            p11.y = p5.y - (int) ((d / 4) * seno((alfa + 30)));
            g.setColor(Color.RED);
            arbolitoDeNavidad(g, p6, p3, alfa - 30, n - 1);
            g.setColor(Color.BLACK);
            arbolitoDeNavidad(g, p7, p4, alfa - 30, n - 1);
            g.setColor(Color.RED);
            arbolitoDeNavidad(g, p8, p5, alfa - 30, n - 1);
            g.setColor(Color.BLACK);
            arbolitoDeNavidad(g, p9, p3, alfa + 30, n - 1);
            g.setColor(Color.RED);
            arbolitoDeNavidad(g, p10, p4, alfa + 30, n - 1);
            g.setColor(Color.BLACK);
            arbolitoDeNavidad(g, p11, p5, alfa + 30, n - 1);
            g.setColor(Color.BLACK);
            arbolitoDeNavidad(g, p1, p5, alfa, n - 1);

        }
    }

}
